package com.sjtu.vfact.textual.model;

import java.util.List;

import com.sjtu.vfact.textual.util.TextUtil;
import com.sjtu.vfact.visualization.base.VfactVertex;

public class TermSet extends VfactVertex implements Comparable<TermSet>{
		
	public TermSet(List<String> terms){
		super(terms);
	}
	
	public TermSet(String[] terms){
		super(terms);
	}
	
	@Override
	public String toString() {
		String termset = "";
		for(int i = 0; i < terms.size(); i++){
			if(i == terms.size() - 1){
				termset += terms.get(i);
			}else{
				termset += terms.get(i) + ", ";
			}
			
		}
		
		return termset;
	}
	
	public TermSet intersect(TermSet termset){
		return new TermSet(TextUtil.<String>intersect(this.terms, termset.getTerms()));
	}
 	
	public String[] toArray(){
		String[] arrays = new String[this.terms.size()];
		for(int i = 0; i < terms.size(); i++){
			arrays[i] = terms.get(i);
		}
		return arrays;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		int sum = 0;
		for(String s : terms){
			sum += s.hashCode();
		}
		result = prime * result + sum;
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TermSet other = (TermSet) obj;
		if (terms == null) {
			if (other.terms != null)
				return false;
		} else if(terms.size() == 0){
			if (other.terms.size() != 0)
				return false;
		} else if(terms.size() != other.getTerms().size()){
			return false;
		} else {
			for(String term : other.getTerms()){
				if(!(terms.contains(term))){
					return false;
				}
			}
		}
		
		return true;
	}

	@Override
	public int compareTo(TermSet ts) {
		return Integer.valueOf(this.getSize()).compareTo(Integer.valueOf(ts.getSize()));
	}
	
}
